
export type keyOptions = {
  name: string, 
  method: string
}

/**
 * 判断是否为api键值
 * 如果键以 目标词缀结尾，则 返回 true
 * @param {string} key - 对象的关键
 * @param {any} value - 键的值
 */
 export function isApi(key: string, value:any){
  let res:any = {}
  // 寻找目标词缀
  if(/_[gGpPdDuU]$/.test(key)) res = keySplit(key) 
  // 值是否为字符串
  if(value?.constructor?.name=='String') res = keySplit(key)
  return res
}

/**
 * 键值分割
 * @param key 
 */
export function keySplit(key:string):keyOptions{
  if(!key) throw new Error(`${key}不是合法的键值`)
  let [name, method] = key.split('_')
  return {
    name, 
    method:method||'g'
  }
}

/**
 * 数组格式转换
 * @param apis 
 */
export function exchange(apis:string[]){
  let res:any = {}
  apis.forEach(i=>{
    let {name} = keySplit(i)
    res[i]=name
  })
  return res
}
